clc;
clear;
%%%%%%%%%%%%%%%Initial population%%%%%%%%%%%
tic;     
N=100;    Pc=0.65;    pm=0.3;   dim=30;
T=200;                              %The number of iterations
%%%%%%%%%%%%%
funlabel=1;                         %Select function
LOW=600;
pop= -LOW + rand(N,dim) * 2 * LOW;  %Population initialization
fit=zeros(100,1);
best = inf;                         %Initial value of optimal fitness
Bfit1=zeros(1,T);  fitcontrast=zeros(100,30); nn=0;
U= (2*LOW) /dim;
pk=0.9;
global R;
tep1=zeros(1,30);
%%%%%%%%%%%%Function evaluation%%%%%%%%%%%%%%%
for it=1:T  
    %if it==100            % 1.9500
    %    pop(1,:)=tep1;
    %end
    for c=1:N
        fit(c,:)=fun(pop(c,:),funlabel); %Calculated fitness
    end
    [fmin,indx]=min(fit);  %min
    if fmin<=best
        best=fmin;         %Optimal fitness value
        optxx=pop(indx,:); %The optima
    end
    Bfit1(it)=best;        % Store the optimal fitness for each generation
    pop(N,:)=optxx;        %The optimal retention
    %%%%%%%%%%%%%% select %%%%%%%%%%%%%%%
    p=fit ./sum(fit);      %%Calculate proportional probability
    q=cumsum(p);
    %for in=1:(N-1)
    %    r=rand;          
    %    tmp=find(r<=q);
    %    nPo(in,:)=pop(tmp(1),:);
    %end
    nPo(N,:)=optxx;           %The optimal retention
    nPo=pop;
    %%%%%%%%%%%%%% cross %%%%%%%%%%%%%%%%%%
    for i=1:2:N
        [x1,y1]=numcross(nPo(i,:),nPo(i+1,:),Pc);
        Nnpo(i,:)=x1;
        Nnpo(i+1,:)=y1;
    end
    Nnpo(N,:)=optxx;          %The optimal retention
    %%%%%%%% mutation %%%%%%
    for i=1:N
        Npo(i,:)=num_mutation(Nnpo(i,:),pm); %(1,30)
    end
    %%%%%%%%%%%%%%%%%%%%%
    Npo(1,:)=optxx;           %Keep the best individuals
    Npo(91:N,:)= -LOW + rand(10,dim) * 2 * LOW;   %%Avoid premature
    pop=Npo;  
    
    %%%Re-evaluate fitness
    for c=1:N
        fit(c,:)=fun(pop(c,:),funlabel); %Calculated fitness
    end
    [fmin,indx]=min(fit);    %min
    if fmin<=best
        best=fmin;         
        optxx=pop(indx,:); 
    end
    
    [fitness, rank]=sort(fit ,'descend');  %Descending order
    pop = pop(rank,:);
    best = fitness(N);      
    optxx = pop(N, :);      %Best individual
    Bfit1(it)= best;        %Optimal fitness
    if it < (T/2)
        R= U - U * (it/(T/2));
    else
        R=0;
    end
    t=1;
    rand2  = rand;                   
    for i=1:N-1
        for j=i+1:N
            Dis(t,it) = norm(pop(i,:)-pop(j,:));  %2 norm
        end
        if Dis(t,it) < R                          %rejection
            randco5 =rand;                       
            pop(i,:)= (-10)* randco5 .* pop(i,:); %collision far
            pop(j,:) =  randco5.* pop(j,:);       %collision
        end
        if Dis(t,it) >= R      %attract
            randco4 = rand; 
            randco3 = rand; 
            if rand2 < pk   %selected at random
                if i<99
                    left= i+1;
                else
                    left = 100;
                end
                rand1 = randi([left 100]); 
                pop(i,:) = pop(rand1,:);
            else
                pop(i,:) = pop(N,:);
            end
        end
        t=t+1;
    end
    for w=1:100
        for u=1:30   %boundary control
            if pop(w,u)> LOW
                pop(w,u)=LOW;
            elseif pop(w,u)< -LOW
                pop(w,u)= -LOW;
            end
        end
    end
    
    pop(1,:)=optxx; 
    %%%Re-evaluate fitness
    for c=1:N
        fit(c,:)=fun(pop(c,:),funlabel); 
    end
    [fmin,indx]=min(fit);  
    if fmin<=best
        best=fmin;         
        optxx=pop(indx,:); 
    end
    
    [fitness, rank]=sort(fit ,'descend');  %descend
    if it > 1               %If the fitness is same, initialize the population   
        if fitcontrast == pop
            nn=nn+1;
            pop=-LOW + rand(N,dim) * 2 * LOW;
        end
    end
    pop(1,:)=optxx;         %The optimal retention
    
    for c=1:N
        fit(c,:)=fun(pop(c,:),funlabel);
    end
    [fmin,indx]=min(fit);  
    if fmin<=best
        best=fmin;         
        optxx=pop(indx,:); 
    end
    Bfit1(it)= best;        %Optimal fitness
    fprintf('%e\n',best);   %Output the optimal fitness value
end
toc;
